﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ZLDC.Topo.Model.Estate;
using ZLDC.Common.Estate;

namespace ZLDC.DAL.Estate
{
                public class UserOptionDAL
    {
                                                                public static UserOptionInfo p_user_option_detail(int tableName, int userid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@tableName", tableName));
            parms.Add(new SqlParameter("@userid", userid));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.fetch_procedure("p_user_option_detail", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            if (db.num_rows == 0)
            {
                return null;
            }
            else
            {
                UserOptionInfo c = new UserOptionInfo();
                c.DltFlg = (db.rows[0]["dltFlg"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["dltFlg"].ToString());
                c.Id = (db.rows[0]["Id"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["Id"].ToString());
                c.UserId = (db.rows[0]["userId"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["userId"].ToString());
                c.TableName = (db.rows[0]["tableName"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0]["tableName"].ToString());
                c.CreateDate = (db.rows[0]["createDate"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[0]["createDate"].ToString());
                c.ModifyDate = (db.rows[0]["modifyDate"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[0]["modifyDate"].ToString());
                c.FieldName = (db.rows[0]["fieldName"] == System.DBNull.Value) ? "" : db.rows[0]["fieldName"].ToString();
                c.Fields = (db.rows[0]["fields"] == System.DBNull.Value) ? "" : db.rows[0]["fields"].ToString();
                c.ModifyUser = (db.rows[0]["modifyUser"] == System.DBNull.Value) ? "" : db.rows[0]["modifyUser"].ToString();
                c.Mark1 = (db.rows[0]["mark1"] == System.DBNull.Value) ? "" : db.rows[0]["mark1"].ToString();
                c.Mark2 = (db.rows[0]["mark2"] == System.DBNull.Value) ? "" : db.rows[0]["mark2"].ToString();
                c.Mark3 = (db.rows[0]["mark3"] == System.DBNull.Value) ? "" : db.rows[0]["mark3"].ToString();
                return c;
            }
        }

                                                                                                                                public static void p_user_option_save(int userId, int tableName, string fieldName, string fields, string modifyUser, int dltFlg, string mark1, string mark2, string mark3, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@userId", userId));
            parms.Add(new SqlParameter("@tableName", tableName));
            parms.Add(new SqlParameter("@fieldName", fieldName));
            parms.Add(new SqlParameter("@fields", fields));
            parms.Add(new SqlParameter("@modifyUser", modifyUser));
            parms.Add(new SqlParameter("@dltFlg", dltFlg));
            parms.Add(new SqlParameter("@mark1", mark1));
            parms.Add(new SqlParameter("@mark2", mark2));
            parms.Add(new SqlParameter("@mark3", mark3));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_user_option_save", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }

    }
}
